package cn.sfrog.project.biz.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizServiceUser;
import cn.sfrog.project.biz.admin.domain.vo.BizServiceVO;
import cn.sfrog.project.biz.admin.service.IBizComboService;
import cn.sfrog.project.biz.admin.service.IBizServiceService;
import cn.sfrog.project.biz.web.vo.combo.AppChangeStatusVO;
import cn.sfrog.system.domain.vo.app.AppServiceQueryVO;
import cn.sfrog.system.domain.vo.app.AppServiceResultVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/api/service")
@Api(value = "/api/service", tags = "APP服务管理相关")
public class AppServiceController extends BaseController {

    @Autowired
    private IBizComboService bizComboService;
    @Autowired
    private IBizServiceService bizServiceService;


    @ApiOperation("获取服务列表")
    @GetMapping("/getServiceList")
    public TableDataInfo<AppServiceResultVO> getComboList(AppServiceQueryVO query){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        query.setCurrentUserId(userId);
        LambdaQueryWrapper<BizServiceUser> lambdaQueryWrapper = new LambdaQueryWrapper<BizServiceUser>()
                .eq(BizServiceUser::getUserId,userId);
        if(StringUtils.isNotBlank(query.getStatus())){
            lambdaQueryWrapper.eq(BizServiceUser::getStatus,query.getStatus());
        }
        List<BizServiceUser> serviceUserList = BizServiceUser.DAO.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(serviceUserList)){
            List<BizServiceUser> serviceUsers = serviceUserList.stream().filter(x -> x.getType().equals(Constants.TYPE_SERVICE)).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(serviceUsers)){
                List<Long> serviceIds = serviceUsers.stream().map(item -> item.getProductId()).collect(Collectors.toList());
                query.setIds(serviceIds);
            }
        }
        startPage();
        List<AppServiceResultVO> serviceList = bizServiceService.selectAppServiceList(query);
        return getDataTable(serviceList);
    }

    @ApiOperation("获取服务详情")
    @GetMapping("/detail/{id}")
    public ApiAjaxResult<BizServiceVO> detail(@PathVariable Long id){
        BizServiceVO bizServiceVO = bizServiceService.selectBizServiceById(id);
        return ApiAjaxResult.success(bizServiceVO);
    }

    @ApiOperation("服务上下架")
    @PostMapping("/changeStatus")
    public ApiAjaxResult changeStatus(@RequestBody @Validated AppChangeStatusVO vo){
        return ApiAjaxResult.success(bizServiceService.updateStatusByApp(vo));
    }
    @ApiOperation("服务列表展示")
    @GetMapping("/getServiceAllList")
    public ApiAjaxResult getServiceAllList(){
        List<BizServiceVO> serviceAllList = bizServiceService.getServiceAllList();
        return ApiAjaxResult.success(serviceAllList);
    }

}
